package bb.lanxing.view.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;

import bb.lanxing.R;
import bb.lanxing.util.DialogUtil;

public class NumberPickerDialog implements View.OnClickListener {
    private AlertDialog alertDialog;
    private float defaultValue;
    private String[] leftDisplayValue;
    private float maxNumber;
    private float miniNumber;
    private NumberPickListener numberPickListener;
    private NumberPicker numberPickerDecimal;
    private NumberPicker numberPickerInteger;
    private FrameLayout panelDecimal;
    private String[] rightDisplayValue;
    private TextView txtSplit;
    private int width;
    private int integer = 0;
    private int decimal = 0;

    public interface NumberPickListener {
        void onDialogDismiss();

        void onNumberPick(float number);
    }

    public int getIntegerPart(float value) {
        return (int) value;
    }

    public boolean isInteger(float value) {
        return 0.0f == value - ((float) ((int) value));
    }

    public NumberPickerDialog(Context context) {
        this.width = 0;
        View inflate = LayoutInflater.from(context).inflate(R.layout.layout_number_picker, (ViewGroup) null);
        TextView textView = (TextView) inflate.findViewById(R.id.tvCancel);
        TextView textView2 = (TextView) inflate.findViewById(R.id.tvDone);
        this.numberPickerInteger = (NumberPicker) inflate.findViewById(R.id.numberPickerInteger);
        this.numberPickerDecimal = (NumberPicker) inflate.findViewById(R.id.numberPickerDecimal);
        this.panelDecimal = (FrameLayout) inflate.findViewById(R.id.panel_decimal);
        this.txtSplit = (TextView) inflate.findViewById(R.id.txt_split);
        textView.setOnClickListener(this);
        textView2.setOnClickListener(this);
        textView.setOnClickListener(this);
        textView2.setOnClickListener(this);
        this.numberPickerInteger.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override // android.widget.NumberPicker.OnValueChangeListener
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPickerDialog.this.integer = newVal;
            }
        });
        this.numberPickerDecimal.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override // android.widget.NumberPicker.OnValueChangeListener
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPickerDialog.this.decimal = newVal;
            }
        });
        AlertDialog create = new AlertDialog.Builder(context).setCancelable(true).setView(inflate).setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override // android.content.DialogInterface.OnCancelListener
            public void onCancel(DialogInterface dialog) {
                if (NumberPickerDialog.this.numberPickListener != null) {
                    NumberPickerDialog.this.numberPickListener.onDialogDismiss();
                }
            }
        }).create();
        this.alertDialog = create;
        DialogUtil.adjustDialogGravityCenter(create);
        try {
            Window window = this.alertDialog.getWindow();
            window.setGravity(80);
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = context.getResources().getDisplayMetrics().widthPixels;
            this.width = attributes.width;
            attributes.y = 0;
            window.setAttributes(attributes);
            window.setWindowAnimations(R.style.BottomDialog_Animation);
        } catch (Exception unused) {
        }
    }

    public AlertDialog getAlertDialog() {
        return this.alertDialog;
    }

    public AlertDialog show() {
        this.alertDialog.show();
        adjustDialogGravityParam();
        return this.alertDialog;
    }

    public void adjustDialogGravityParam() {
        Window window = this.alertDialog.getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.width = this.width;
        attributes.gravity = Gravity.BOTTOM;//.BOTTOM;//81;
        window.setAttributes(attributes);
    }

    public AlertDialog dimiss() {
        this.alertDialog.dismiss();
        return this.alertDialog;
    }

    @Override // android.view.View.OnClickListener
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvCancel) {
            this.alertDialog.dismiss();
            NumberPickListener numberPickListener = this.numberPickListener;
            if (numberPickListener != null) {
                numberPickListener.onDialogDismiss();
            }
        } else if (id != R.id.tvDone) {
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(this.integer);
            if (this.decimal != 0) {
                sb.append(".");
                sb.append(this.decimal);
            }
            NumberPickListener numberPickListener2 = this.numberPickListener;
            if (numberPickListener2 != null) {
                numberPickListener2.onNumberPick(Float.valueOf(sb.toString()).floatValue());
            }
            dimiss();
        }
    }

    public void setUp() {
        if (isInteger(this.maxNumber) && isInteger(this.miniNumber)) {
            setDecimalVisibility(8);
        } else {
            setDecimalVisibility(0);
            int decimalPart = getDecimalPart(this.miniNumber);
            int decimalPart2 = getDecimalPart(this.maxNumber);
            float f = this.defaultValue;
            setRightRange(decimalPart, decimalPart2, f == 0.0f ? getDecimalPart(this.miniNumber) : getDecimalPart(f));
        }
        int integerPart = getIntegerPart(this.miniNumber);
        int integerPart2 = getIntegerPart(this.maxNumber);
        float f2 = this.defaultValue;
        setLeftRange(integerPart, integerPart2, f2 == 0.0f ? getIntegerPart(this.miniNumber) : getIntegerPart(f2));
    }

    public void setLeftRange(int start, int end, int defaultValue) {
        this.numberPickerInteger.setMinValue(start);
        this.numberPickerInteger.setMaxValue(end);
        this.numberPickerInteger.setValue(defaultValue);
        if (defaultValue != 0.0f) {
            start = defaultValue;
        }
        this.integer = start;
        this.numberPickerInteger.setDescendantFocusability(393216);
    }

    public void setRightRange(int start, int end, int defaultValue) {
        this.numberPickerDecimal.setMinValue(start);
        this.numberPickerDecimal.setMaxValue(end);
        this.numberPickerDecimal.setValue(defaultValue);
        this.decimal = start;
        this.numberPickerDecimal.setDescendantFocusability(393216);
    }

    public float getMaxNumber() {
        return this.maxNumber;
    }

    public void setMaxNumber(float maxNumber) {
        this.maxNumber = maxNumber;
        setUp();
    }

    public float getMiniNumber() {
        return this.miniNumber;
    }

    public void setMiniNumber(float miniNumber) {
        this.miniNumber = miniNumber;
        setUp();
    }

    public float getDefaultValue() {
        return this.defaultValue;
    }

    public void setDefaultValue(float defaultValue) {
        this.defaultValue = defaultValue;
        setUp();
    }

    public void setDecimalVisibility(int visibility) {
        this.panelDecimal.setVisibility(visibility);
    }

    public int getDecimalPart(float value) {
        String[] split = String.valueOf(value).split("\\.");
        if (split.length > 0) {
            return Integer.parseInt(split[1]);
        }
        return 0;
    }

    public NumberPickerDialog setNumberPickListener(NumberPickListener numberPickListener) {
        this.numberPickListener = numberPickListener;
        return this;
    }

    public void setLeftDisplayTimeZoneValue(final String[] displayValue, int defaultValue) {
        this.txtSplit.setText(":");
        this.leftDisplayValue = displayValue;
        this.numberPickerInteger.setMinValue(0);
        this.numberPickerInteger.setMaxValue(displayValue.length - 1);
        this.numberPickerInteger.setDisplayedValues(displayValue);
        this.numberPickerInteger.setValue(defaultValue);
        this.numberPickerInteger.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override // android.widget.NumberPicker.OnValueChangeListener
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                if (newVal == 0 || newVal == displayValue.length - 1) {
                    NumberPickerDialog.this.numberPickerDecimal.setMinValue(0);
                    NumberPickerDialog.this.numberPickerDecimal.setMaxValue(0);
                    NumberPickerDialog.this.numberPickerDecimal.setDisplayedValues(new String[]{"00"});
                    NumberPickerDialog.this.numberPickerDecimal.setValue(0);
                    NumberPickerDialog.this.decimal = 0;
                } else {
                    NumberPickerDialog.this.numberPickerDecimal.setMinValue(0);
                    NumberPickerDialog.this.numberPickerDecimal.setDisplayedValues(NumberPickerDialog.this.rightDisplayValue);
                    NumberPickerDialog.this.numberPickerDecimal.setMaxValue(NumberPickerDialog.this.rightDisplayValue.length - 1);
                    NumberPickerDialog.this.numberPickerDecimal.setValue(0);
                    NumberPickerDialog.this.decimal = 0;
                }
                NumberPickerDialog.this.integer = newVal;
            }
        });
        this.integer = defaultValue;
        this.numberPickerInteger.setDescendantFocusability(393216);
    }

    public void setRightDisplayTimeZoneValue(String[] displayValue, int defaultValue) {
        this.rightDisplayValue = displayValue;
        int i = this.integer;
        if (i == 0 || i == this.leftDisplayValue.length - 1) {
            this.numberPickerDecimal.setMinValue(0);
            this.numberPickerDecimal.setMaxValue(0);
            this.numberPickerDecimal.setDisplayedValues(new String[]{"00"});
            this.numberPickerDecimal.setValue(0);
            this.decimal = 0;
        } else {
            this.numberPickerDecimal.setMinValue(0);
            this.numberPickerDecimal.setDisplayedValues(displayValue);
            this.numberPickerDecimal.setMaxValue(displayValue.length - 1);
            this.numberPickerDecimal.setValue(defaultValue);
            this.decimal = defaultValue;
        }
        this.numberPickerDecimal.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override // android.widget.NumberPicker.OnValueChangeListener
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                NumberPickerDialog.this.decimal = newVal;
            }
        });
        this.numberPickerDecimal.setDescendantFocusability(393216);
    }
}
